# 每日需执行任务
import  data_funcs as df
import _params
import pandas as pd
from os import path
import _data_collection as dc
import _stock_selection as ss

# 刷新股票列表并更新股票行情
def _init_get_stock_hq():
    print("开始更新股票行情数据!")
    # 获取数据
    # 获取股票列表
    df.data_stock_list()
    # 获取待处理的股票列表
    if path.exists(_params.__STOCK_LIST_FULL_FILE_PATH):
        stock_list = pd.read_excel(_params.__STOCK_LIST_FULL_FILE_PATH, converters={'ticker':str})
    else:
        print("未获取到待处理的股票列表，请初始化")
        exit()
    # 获取股票行情，并保存至文件（单个股票单个文件）
    df.data_stock_hq(stock_list['ticker'])
    print("更新股票行情数据完成!")

def _filter_by_pb_mv(tradeDate):
    print("开始FAMA筛选!")
    date = dc.collect_newest_tradedate(tradeDate)
    list = ss.fama_with_pb_mv(100,date)
    list.to_excel("analysis/fama/fama_" + date + ".xlsx", index=False)
    print("--完成FAMA筛选!")

def _filter_by_kdj(today,kdjN=9, kdjM1=3, kdjM2=3, saveFlag=False):
    print("开始KDJ筛选!")
    list = ss.kdj_for_buy_signal(kdjN, kdjM1, kdjM2,saveFlag)
    list.to_excel("analysis/kdj/golden_"+today+".xlsx")
    print("完成KDJ筛选!")

#控制运行那些任务
RunFunc = '123'
Today = '20160421'
if '1' in RunFunc:
    # 运行获取行情数据任务
    _init_get_stock_hq()
if '2' in RunFunc:
    # 运行FAMA筛选
    _filter_by_pb_mv(Today)
if '3' in RunFunc:
    # 运行KDJ筛选
    _filter_by_kdj(today=Today)